define(
	[
		'require'
	],
	function(require){
		function dateCellFormatter(args, column){
			if (args.row.type & $.wijmo.wijgrid.rowType.data) {
				var data = args.row.data,
					dataKey = column.dataKey,
					formatedTime = APM.util.formatDate(data[dataKey], "yyyy-MM-dd H:mm:ss");
				data["_formatted_"+dataKey] = formatedTime;
				args.$container.append(formatedTime);
				return true;
			}
		}  
		var Controller = Ember.Controller.extend({

			errorcodes: Ember.A(),

			channelValue: '',

			pageSize: 10,

			transactionChannel: Ember.A(),
			/*
				列配置
			*/
			perfColumns: [
				{
					headerText: "交易渠道名称",
					dataKey:"channelName",
					cellFormatter:function(args){
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var data = args.row.data.channelName;
							if(data == 0){
								data = 0;
							}else if(!data){
								data = "-";
							}
							var cb = $("<div ></div>").text(data);
							cb.appendTo(args.$container);
							return true;
						}
					}
				},
				{
					headerText: "交易量",
					dataKey:"businessNumberTotal",
					cellFormatter:function(args){
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var data = args.row.data.businessNumberTotal;
							if(data == 0){
								data = 0;
							}else if(!data){
								data = "-";
							}
							var cb = $("<div ></div>").text(data);
							cb.appendTo(args.$container);
							return true;
						}
					}
				},
				{
					headerText: "平均响应时间",
					dataKey:"avgResponseTime",
					_fmtType: "transform_time",
					cellFormatter: APM.util.indicatorCellFormatter
				},
				{
					headerText: "成功率",
					dataKey:"successRate",
					cellFormatter:function(args){
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var data = args.row.data.successRate;
							if(data == 0){
								data = 0;
							}else if(!data){
								data = "-";
							}
							var cb = $("<div ></div>").text(data);
							cb.appendTo(args.$container);
							return true;
						}
					}
				},
				{
					headerText: "错误数",
					dataKey:"errorCount",
					cellFormatter:function(args){
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var data = args.row.data.errorCount;
							if(data == 0){
								data = 0;
							}else if(!data){
								data = "-";
							}
							var cb = $("<div ></div>").text(data);
							cb.appendTo(args.$container);
							return true;
						}
					}
				}
			],

			requireData: function(){
				var ctrl = this,
					serviceId = ctrl.get("model.id"),
					typeValue = ctrl.get("typeValue");

				var params = {
					"serviceId":serviceId,
					"keyWord": this.get("keyWord"),
				    "startDate": +this.get("startDate"),
					"endDate": +this.get("endDate"),
					"transactionType": typeValue,
					"pageIndex": this.get("pageIndex"),
				    "pageSize": this.get("pageSize"),
				};

				if( isLocalDebugModel ){
					params = undefined;
				}

				return this.store.ajax("GET", "trades/channel", params)
				.then(function(response){
					var result = response.result;
					result.forEach(function(node){
						if(node.successRate == 0){
							node.successRate = 0+"%";
						}else if(!node.successRate){
							node.successRate = null;
						}else{
							node.successRate = node.successRate*100+"%";
						}
					});
					
					if (result.length == 0) {
						
						ctrl.set("pageIndex",0);
						ctrl.set("pageSize",10);
						ctrl.set("totalRows",0);
					} else {
						ctrl.set("pageIndex",response.meta.pageIndex);
						ctrl.set("pageSize",response.meta.pageSize);
						ctrl.set("totalRows",response.meta.total);
					}
					ctrl.set("errorcodes",result);
					
					return response;
				});
			},

			transactionTypeRequireAlerts: function(params){
				return this.store.ajax("GET","transaction/channel",params);
			},

			isOnce:true,

			actions: {
				search: function() {
					this.set("pageIndex", 0);
					this.requireData();
				},

				pageIndexChanged: function(e, data, view){
					var pageNo = data.newPageIndex;
					this.set("pageIndex", pageNo);
					this.requireData();
				},
				timeRangesChange: function(timeRange){
					var ctrl = this,
						locationStart = this.get("locationStart");
					
					// if(!isSelectedTime){
					ctrl.set("startDate", timeRange.startDate);
					ctrl.set("endDate", timeRange.endDate);
					// }else{
						// var view = Broada.getView("businessTimeRangesTopo");
						// if(this.get("clickTimePoint") != null){
						// 	this.set("locationStart",true);
						// 	var date = new Date(this.get("clickTimePoint"));
						// 	if(typeof view != "undefined"){
						// 		var range = view.getTimeRange().endDate - view.getTimeRange().startDate;	
						// 		if(range == 60000){
						// 			this.set("startDate", date);
						// 			this.set("endDate", new Date(date.getTime()+range));
						// 		}else{
						// 			this.set("startDate", new Date(date-range));
						// 			this.set("endDate", date);
						// 		}
						// 	}
						// 	typeof view != "undefined"?view.set("btnTxt", date?APM.util.formatDate(date, "f"):null):null;
						// 	this.requireData();
						// }
					// };
					
					ctrl.set("pageIndex", 0);
					ctrl.requireData();
				}
			}
		});

		APM.ServiceServerNodeserverChannelController = Controller;
		return Controller;
	}
);